package tr.edu.ozyegin.ccrg.monitor.network;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class NetworkUsage {
    
    public static void main(String []args) throws IOException, FileNotFoundException, InterruptedException{
        new NetworkUsage();
        
    }
    private String percentage = "";
    private ArrayList<Nic> nicList = new ArrayList<Nic>();
    public String getPercentage(){
    	return this.percentage;
    }
    private String setPercentage(String percentage){
    	return this.percentage = percentage;
    }
    public NetworkUsage() throws FileNotFoundException, IOException, InterruptedException{
        this.InitCards();
        this.StartMonitoring();
    }
        @SuppressWarnings("deprecation")
        private void InitCards() throws FileNotFoundException, IOException{
            FileInputStream fis = new FileInputStream("/proc/net/dev");
            DataInputStream dis = new DataInputStream(fis);
            String line = null;
            while((line = dis.readLine())!=null){
		if(line.indexOf(":") >= 0){
			String[] splitted = this.NormalizeString(line).replace(": ", " ").replace(":", " ").split(" ");
			Nic control = this.findNic(splitted[0]);
                        if(control != null){
                            //System.out.println(Long.parseLong(splitted[1]));
                            control.setReceived(Long.parseLong(splitted[1]));
                            control.setTransmitted(Long.parseLong(splitted[9]));
                        }
                        else{
                            Nic nic = new Nic(splitted[0],1024*1024*54);
                            nic.setReceived(Long.parseLong(splitted[1]));
                            nic.setTransmitted(Long.parseLong(splitted[9]));
                            this.nicList.add(nic);
                        }
                        /*System.out.println("Name: " +splitted[0]);
			System.out.println("Received: " +splitted[1]);
			System.out.println("Sent: " +splitted[9]);
			System.out.println();*/
		}
            }
            dis.close();
            fis.close();
        }
        
        private Nic findNic(String name){
            if(this.nicList.isEmpty()) return null;
            for(int i = 0; i < this.nicList.size(); i++){
                Nic nic = this.nicList.get(i);
                if(nic.getName().compareTo(name) == 0) return nic;
            }
            return null;
        }
        
	@SuppressWarnings("deprecation")
	public void StartMonitoring() throws IOException, InterruptedException{
            this.InitCards();
            while(true){ 
                Thread.sleep(1000);
                for(int i = 0; i < this.nicList.size(); i++){
                    Nic nic = this.nicList.get(i);
                    //System.out.println(nic.getReceived() + " " + nic.getCapacity());
                    double received = (double)nic.getReceived()/(double)(1024);
                    double transmitted = (double)nic.getTransmitted()/(double)(1024);
                    System.out.println(nic.getName() + " Received " + received + " KB/s Transmitted " + transmitted + " KB/s");
                }
                System.out.println();
                System.out.flush();
                this.InitCards();
            }
	}
	private String NormalizeString(String toNormalize){
		if(toNormalize == null) return null;
		String result = "";
		boolean flag = true;
		char tmp = ' ';
		for(int i = 0; i< toNormalize.length(); i++){
			if(flag == true && (tmp = toNormalize.charAt(i)) != ' '){
				flag = false;
				result += tmp;
			}
			else if(flag == false && (tmp = toNormalize.charAt(i)) != ' ')result += tmp;
			else if(flag == false && (tmp = toNormalize.charAt(i)) == ' '){
				flag = true;
				result += tmp;
			}
		}
		return result;
	}
	
}
